﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using V5_Model;
using V5_Common;
using V5_BLL;
using System.Data;

namespace V5_EShop._Template.Default
{
    public partial class search : BasicPage
    {
        protected ModelProductClass productclass = new ModelProductClass();
        public int _classid = 0, _showid = 0, _orderid = 0, _pageid = 0;
        public string searchkey = "";
        public int productclassid = 0, low = 0, high = 0;

        protected override void OnInit(EventArgs e)
        {
            this.rpList2.ItemDataBound += new RepeaterItemEventHandler(rpList2_ItemDataBound);
            base.OnInit(e);
        }

        void rpList2_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            HiddenField hf = (HiddenField)e.Item.FindControl("hfProductFields");
            Repeater rp = (Repeater)e.Item.FindControl("rpSingleList");
            if (rp != null)
            {
                DataTable dt = new DataTable();

                dt.Columns.Add(new DataColumn("m1"));
                dt.Columns.Add(new DataColumn("m2"));
                if (hf.Value.Trim().Length > 0)
                {
                    string[] sarr1 = hf.Value.Split(new string[] { "||" }, StringSplitOptions.None);
                    foreach (string s1 in sarr1)
                    {
                        string[] sarr2 = s1.Split(new string[] { ":" }, StringSplitOptions.None);
                        DataRow dr = dt.NewRow();
                        dr["m1"] = sarr2[0];
                        dr["m2"] = sarr2[1];
                        dt.Rows.Add(dr);
                    }
                }
                rp.DataSource = dt;
                rp.DataBind();
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                searchkey = Input.SetString(Request["txtsearchkey"]);
                productclassid = Input.SetInt(Request["productclassid"]);
                if (Input.SqlFilter(searchkey))
                {
                    Response.Redirect("/index.aspx");
                }
                if (productclassid > 0)
                {
                    _classid = productclassid;
                }
                _showid = Input.SetInt(Request["showid"]);
                if (_showid == 0)
                {
                    _showid = 1;
                }
                _orderid = Input.SetInt(Request["orderid"]);
                _pageid = Input.SetInt(Request["pageid"]);
                if (_pageid == 0)
                {
                    _pageid = 1;
                }
                low = Input.SetInt(Request["low"]);
                high = Input.SetInt(Request["high"]);
                this.Bind_rpList();
                productclass = new BLLProductClass().GetModel(_classid);
            }
        }

        private void Bind_rpList1(DataTable dt)
        {
            this.rpList1.Visible = true;
            this.rpList2.Visible = false;
            this.rpList1.DataSource = dt;
            this.rpList1.DataBind();
        }

        private void Bind_rpList2(DataTable dt)
        {
            this.rpList2.Visible = true;
            this.rpList1.Visible = false;
            this.rpList2.DataSource = dt;
            this.rpList2.DataBind();
        }
        private void Bind_rpList()
        {
            string PageUrl = "/Search.aspx?txtsearchkey=" + searchkey
                + "&productclassid=" + _classid
                + "&showid=" + _showid
                + "&orderid=" + _orderid
                + "&low=" + low
                + "&high=" + high
                + "&pageid={0}";
            int count = 0;
            int IsDesc = 0;
            string strWhere = " ID>0 ", strOrder = " ID ";
            Pager.CurrentPageIndex = _pageid;
            //开始过滤参数
            if (_classid > 1)
            {
                strWhere += " and ClassID=" + Utils.FilterInjection(_classid);
            }
            //

            switch (_showid)
            {
                case 1:
                    Pager.PageSize = 12;
                    break;
                case 2:
                    Pager.PageSize = 6;
                    break;
            }
            //销量 价格 排序 默认为1
            switch (_orderid)
            {
                case 1:
                    strOrder = "ID";
                    IsDesc = 1;
                    break;
                case 2://销售量排序
                    strOrder = "ID";
                    IsDesc = 1;
                    break;
                case 3:
                    strOrder = " convert(int,productmemberprice) ";
                    IsDesc = 0;
                    break;
                case 4:
                    strOrder = " convert(int,productmemberprice) ";
                    IsDesc = 1;
                    break;
            }
            string strOrder2 = strOrder;
            if (IsDesc == 0)
            {
                strOrder2 += " desc ";
            }
            else
            {
                strOrder2 += "asc";
            }
            if (searchkey != "" && searchkey.Length > 0)
            {
                strWhere += "  and ProductName like '%" + Utils.FilterInjection(searchkey) + "%' ";
            }

            DataTable dt = new BLLProduct().GetList(
                Pager.PageSize,
                Pager.CurrentPageIndex,
                strWhere,
                strOrder,
                IsDesc,
                out count
                ).Tables[0];
            this.Pager.RecordCount = count;
            this.Pager.UrlRewritePattern = PageUrl;

            switch (_showid)
            {
                case 1:
                    Bind_rpList1(dt);
                    break;
                case 2:
                    Bind_rpList2(dt);
                    break;
            }

        }
        protected void Pager_PageChanged(object sender, EventArgs e)
        {
            this.Bind_rpList();
        }
    }
}
